********************************************************************************
*DYNAMIC IMPACTS OF PRICING GROUNDWATER
*Bruno, Jessoe, Hanemann in JAERE
*
*THIS CODE IS FOR THE EVENT STUDY ON LAND USE
*Produces Figures 5 and 6
********************************************************************************
clear all
capture log close
set more off

*SELECT OUTPUT DATE
global outputdate = "20231125"		 

*SET DIRECTORY
cd  "D:\Ellen\Dropbox\Pajaro_AgInnovation" 

use "Data\Land_clean_20230615.dta", clear

log using "Submission\JAERE\Replication_Code\Log\Land_event_study_log.log", replace

sort year quarter

********************************************************************************
*1. ANNUAL EVENT STUDY: RAW DATA 
********************************************************************************

*Balance the panel
bysort parcelnum: gen ntime = [_N]
sum ntime
keep if ntime==`r(max)'

********************************************************************************
*1.1 Generate dummy variables
********************************************************************************

*Generate event periods 
gen event_time = 0
forvalues j = 1/11 {
replace event_time = -`j' if year == 2012 - `j'
replace event_time = `j' if year == 2012 + `j'
}


*Generate pre period interaction terms
forvalues j = 1/3 {
gen minus`j' = 0
replace minus`j' = 1 if inside == 1 & event_time == -`j'
}

*Generate post period interaction terms
forvalues j = 1/9 {
gen plus`j' = 0
replace plus`j' = 1 if inside == 1 & event_time == `j'
}

*Generate treatment event interaction
generate event_treatment = 0
replace event_treatment  = 1 if inside == 1 & event_time==0


*Drop missing variables
foreach var of varlist plus* minus* {
summ `var'
   if `r(sum)' == 0  {
            drop `var'
           }
       else { 
               }
         }


*UNCONDITIONAL EVENT STUDY - TOTAL AG LAND
preserve
xtreg acres_agtot plus* i.year#i.county event_treatment i.inside minus3 minus1, fe cluster(parcelnum)
mat coeff = [  _b[minus3] \ _b[minus1] \ _b[event_treatment] \ _b[plus1] \ _b[plus2] ///
			 \ _b[plus3] \ _b[plus4] \_b[plus5] \_b[plus6] \ _b[plus7] \ _b[plus8] ]		
mat se = [ _se[minus3] \ _se[minus1]  \ _se[event_treatment] \ _se[plus1] \ _se[plus2] \ _se[plus3] ///
\ _se[plus4] \_se[plus5] \_se[plus6] \_se[plus7] \_se[plus8] ]
mat upper=coeff+((1.96)*se)
mat lower=coeff-((1.96)*se)
mat x=(-3\-1\0\1\2\3\4\5\6\7\8)
mat data=[coeff,upper,lower,x]
svmat data
rename data1 coeff
rename data2 upper
rename data3 lower
rename data4 x

matrix colnames data=beta loCI highCI year
matrix list data
putexcel set "Tables\land_eventstudy_cy_$outputdate.xlsx", sheet(agtotal) replace
putexcel A1 = matrix(data)
clear
svmat data, names(col)

replace year = 2009 if year ==-3
replace year = 2010 if year ==-2
replace year = 2011 if year ==-1
replace year = 2012 if year ==0
replace year = 2013 if year ==1
replace year = 2014 if year ==2
replace year = 2015 if year ==3
replace year = 2016 if year ==4
replace year = 2017 if year ==5
replace year = 2018 if year ==6
replace year = 2019 if year ==7
replace year = 2020 if year ==8


twoway (rcap loCI highCI year if year <2016) (scatter beta year if year <2016, msymbol(triangle) msize(small)) if year>2010 , ///
ytitle(Agricultural Land (Acres) )  ///
 scheme(s1color) legend(label (1 95% CI) label (2 Difference Inside and Outside)) ///
	xtitle(Year) yline(0, lcolor(black)) xlab(2011(1)2015) ylab(-6(2)6)

graph save "Figures\ES_agtotal_cy_$outputdate.gph", replace
graph export "Figures\ES_agtotal_cy_$outputdate.png", replace
restore



*UNCONDITIONAL EVENT STUDY - VEG + STRAW 
preserve

xtreg vegstraw plus* i.year#i.county event_treatment i.inside minus3 minus1, fe cluster(parcelnum)
mat coeff1 = [  _b[minus3] \ _b[minus1] \ _b[event_treatment] \ _b[plus1] \ _b[plus2] \ _b[plus3] ///
			\ _b[plus4] \_b[plus5] \_b[plus6] \_b[plus7] \_b[plus8] ]		
mat se1 = [ _se[minus3] \ _se[minus1] \ _se[event_treatment] \ _se[plus1] \ _se[plus2] ///
\ _se[plus3] \ _se[plus4] \_se[plus5] \_se[plus6] \_se[plus7] \_se[plus8] ]
mat upper1=coeff1+((1.96)*se1)
mat lower1=coeff1-((1.96)*se1)
mat x=(-3\-1\0\1\2\3\4\5\6\7\8)

reg otherag plus* event_treatment i.inside minus3 minus1 ,cluster(parcelnum)
mat coeff2 = [   _b[minus3] \ _b[minus1] \ _b[event_treatment] \ _b[plus1] \ _b[plus2] \ _b[plus3] ///
			 \ _b[plus4] \_b[plus5]  \_b[plus6] \_b[plus7] \_b[plus8] ]			
mat se2 = [ _se[minus3] \ _se[minus1] \ _se[event_treatment] \ _se[plus1] \ _se[plus2] ///
 \ _se[plus3] \ _se[plus4] \_se[plus5] \_se[plus6] \_se[plus7] \_se[plus8] ]
mat upper2=coeff2+((1.96)*se2)
mat lower2=coeff2-((1.96)*se2)
mat data=[coeff1,upper1,lower1,x, coeff2, upper2, lower2]

svmat data
rename data1 coeff_1
rename data2 upper_1
rename data3 lower_1
rename data4 x
rename data5 coeff2
rename data6 upper2
rename data7 lower2

matrix colnames data=beta1 loCI1 highCI1 year beta2 loCI2 highCI2
matrix list data
putexcel set "Tables\land_eventstudy_cy_$outputdate.xlsx", sheet(crop) modify
putexcel A1 = matrix(data)
clear
svmat data, names(col)

replace year = 2009 if year ==-3
replace year = 2010 if year ==-2
replace year = 2011 if year ==-1
replace year = 2012 if year ==0
replace year = 2013 if year ==1
replace year = 2014 if year ==2
replace year = 2015 if year ==3
replace year = 2016 if year ==4
replace year = 2017 if year ==5
replace year = 2018 if year ==6
replace year = 2019 if year ==7
replace year = 2020 if year ==8

twoway (rcap loCI1 highCI1 year if year <2016) (scatter beta1 year if year <2016) if year>2010, ///
ytitle(Acres) scheme(s1color) xtitle(Year) yline(0, lcolor(black)) xlab(2011(1)2015) ylab(-10(2)10) ///
legend(label (1 95% CI) label (2 Difference Inside and Outside (Veg + Straw)) label (3 95% CI) label (4 Difference Inside and Outside (Other Ag)))	

graph save "Figures\ES_vegstraw_cy_$outputdate.gph", replace
graph export "Figures\ES_vegstraw_cy_$outputdate.png", replace
restore

log close
